﻿#include "HttpAccess.h"
#include "Logger.h"
HttpAccess::HttpAccess(QObject* parent)
    : QObject(parent)
{}
HttpAccess::~HttpAccess() {}
void HttpAccess::Get(const QUrl& url)
{
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::UserAgentHeader, g_UserAgent[0]);
    QNetworkReply* reply = m_manager.get(request);
    QObject::connect(reply, &QNetworkReply::finished, [this, reply]() {
        if (reply->error() == QNetworkReply::NoError) {
            QByteArray data       = reply->readAll();
            QString    htmlConent = QString::fromUtf8(data);
            emit       finished(htmlConent);
        }
        else {
            int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
            HandleError(statusCode);
            emit error(reply->errorString());
        }
    });
}
void HttpAccess::HandleError(int statusCode)
{
    Logger::GetInstance().LogError(QString::number(statusCode));
}